<?php

		class guestAction extends userAction {
			private $feedSetting = false; 
			protected function _initialize() {
				parent::_initialize();
				$this->feedSetting = getAddFeedSetting($this->userInfo['id'],"guestaction"); 
			}
			
			public function index(){
				isset($_GET["u"]) ? $u = safeRequest($_GET["u"]) : $u = $this->userInfo['id'];
				$this->rank =  getRank($this->userInfo['id'],$u,"guest");
				if(!$this->rank){
					redirect(__APP__."/rank/norank/u/".$u);
					return false;
				}
				isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1; 
				import("ORG.Util.Page"); 
				$dao = D("guest");
				if(isset($_GET["t"])){
					$t = safeRequest($_GET["t"]);
					$guestList = D("guest")->findAll("(userid=$u and guestid=$t) or (userid=$t and guestid=$u)","*","regtime desc");
					$toname = getUsernameByUserid($t);
					$this->assign("toname",$toname);
					$this->assign("guestList",$guestList);
					$this->assign("myId",$this->userInfo['id']);
					if($u != $this->userInfo['id']){
						$userInfo = D("user")->find("id=".$u,"id,username");
						$this->assign("userInfo",$userInfo);
					}
					$this->display("chat");
				}else{
					$guestCount = $dao->count("userid=".$u,"id");
					$p 	= 	new Page($guestCount,"20"); 
					$p	->	setConfig('header' ,L("guest_page_title")); 
					$guestList = $dao->findAll("userid=".$u,"*","regtime desc",$p->firstRow.','.$p->listRows);
					$page	= 	$p->show();
					$this	->	assign("page",$page); 
					$this->assign("guestList",$guestList);
					$this->assign("myId",$this->userInfo['id']);
					if($u != $this->userInfo['id']){
						$userInfo = D("user")->find("id=".$u,"id,username");
						$this->assign("userInfo",$userInfo);
					}
					$this->display();
				}
			}

			public function write(){
				$this->display();
			}

			public function insert(){
				$dao = D("guest");
				$u = safeRequest($_POST["userid"]);
				$content = str_filter($_POST["content"]);
				$dao->userid = $u;
				$dao->guestid = $this->userInfo['id'];
				$dao->content = $content;
				$dao->regtime = time();
				$id = $dao->add();
				if($id>0){
					//添加通知
					D("cache")->setField("guestCount","(guestCount+1)","userid=".$u);
					addNotify($u,$this->userInfo['id'],"guest",$id);
					if($this->feedSetting){
						//添加动态
						addFeed($this->userInfo['id'],"guest",$content,$id,$u);
					}
					isset($_POST["ajax"]) ? print $this->userInfo['id'].",".$this->userInfo['username'].",".toDate($dao->regtime) : $this->success(L("geust_success"));
				}else{
					isset($_POST["ajax"]) ? print "error" : $this->error(L("guest_error"));
				}
			}

			public function del(){
				$id = safeRequest($_GET["id"]);
				$u = safeReqiest($_GET["u"]);
				if(D("guest")->delete("id=".$id." and guestid=".$this->userInfo['id'])){
					D("cache")->setField("guestCount","(guestCount+1)","userid=".$u);
					delFeed(0,$this->userInfo['id'],"guest",$id);//删除动态
					isset($_GET["ajax"]) ? print "success" : $this->success(L("delete_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("delete_error"));
				}
			}

			public function chat(){
				$userid = safeRequest($_GET["u"]);
				$myid = $this->userInfo['id'];
				$list = D("user")->findAll("(userid=$myid and postid=$userid) or (userid=$userid and postid=$myid)","*","regtime desc");
				$this->assign("list",$list);
				$this->display();
			}
		}
?>